import { http } from '../../utils/net.js'
import { authChannel } from '../../utils/util.js'
import { normalInput } from '../../utils/inputBothBind.js'

Page({
  data: {
    topStepArr: ["1.支付信息", "2.通道选择", "3.收款信息"],
    channelList:[],
    money:null,
    bcId:null,
    currentChannel: null,
    cardInfo:null,
    smsCode:'',
    orderNum:''
  },
  normalInput:normalInput,
  toSendAuth2() {
    if(this.data.smsCode) {
      http({
        url:"/api/index/index?method=confirm_channel_authentication",
        data: {
          order_num: this.data.orderNum,
          sms_code: this.data.smsCode
        },
        successCb: (res) => {

        },
        failCb: (err) => {
          wx.showModal({
            title: "鉴权失败",
            content: err.msg,
            showCancel: false,
            confirmText: '我知道了',
            success: function(res) {
              if(res.confirm) {
                // TODO
                console.log(res)
              }
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '请填写验证码',
        icon: 'none'
      })
    }
  },
  toCancelAuth2() {
    this.selectComponent("#auth").close()
  },
  toStep3() {
    if (!this.data.currentChannel) {
      wx.showToast({
        title: '你还未选择通道',
        icon: "none"
      })
      return
    }
    let mon = parseFloat(this.data.money)
    let rate = parseFloat(this.data.currentChannel.cloud_brush_rate)
    let fixed = parseFloat(this.data.currentChannel.cloud_brush_fixed)
    let outMoney = mon - mon * rate - fixed

    wx.navigateTo({
      url: `/pages/cloudSwiperStep3/index?money=${this.data.money}&bcId=${this.data.bcId}&channelId=${this.data.currentChannel.pc_id}&outMoney=${outMoney}`
    })
  },
  toSelectOrOpenChannel(event) {
    console.log(event.currentTarget.dataset.hi)
    const tmp = event.currentTarget.dataset.hi
    if (tmp.is_dredge) {
      this.setData({
        currentChannel: tmp
      })
    } else {
      authChannel({ channelId: tmp.pc_id, cardInfo: this.data.cardInfo }).then(res => {
        console.log("page", res)
        if (res.code == 0 || res.code == -1) {
          this.getDataFun()
        } else if (res.code == 1) {
          this.setData({
            orderNum: res.data
          })
          this.selectComponent("#auth").open()
        } else if (res.code == 2) {
          wx.navigateTo({
            url: "/pages/web/index",
            success: (res1) => {
              res1.eventChannel.emit('acceptDataFromOpenerPage', {
                url:res.data,
                callback:() => {
                  this.getDataFun()
                }
              })
            },
          })
        } else if (res.code == 3) {
          wx.navigateTo({
            url: "/pages/web/index",
            success: (res1) => {
              res1.eventChannel.emit('acceptDataFromOpenerPage', {
                url: res.url,
                callback: () => {
                  this.getDataFun()
                  http({
                    url:"/api/index/index?method=del_html",
                    data: {
                      path: res.path
                    },
                    successCb: (res2) => {},
                    failCb: (err2) => {}
                  })
                }
              })
            },
          })
        }
      })
    }
  },
  getDataFun() {
    http({
      url: "/api/index/index?method=channel_list",
      data: {
        bc_id: this.data.bcId
      },
      isShowLoading: true,
      successCb: (res) => {
        res.data.forEach((i) => {
          if (i.default_channel) {
            this.setData({
              currentChannel:i
            })
          }
        })
        this.setData({
          channelList: res.data
        })
      }
    })
  },
  onLoad: function (options) {
    let that = this
    this.selectComponent("#auth").open()
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      that.setData({
        money: data.money,
        bcId: data.cardInfo.bc_id,
        cardInfo:data.cardInfo
      })
    })
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDataFun()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})